<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Comment extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->UserModel = model( 'User.User' );
        $this->CommentModel = model( 'Post.Comment' );
    }

    // 评论主页

    public function index() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'comment.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'comment.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'comment.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'post.title' ] = [ 'like', "%$keywords%" ];
        }

        // 查询评论总数
        $count = $this->CommentModel->count();

        $commentlist = $this->CommentModel->with( [ 'post', 'user' ] )->where( $where )->order( 'createtime', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $commentlist as $item ) {
            if ( $item[ 'like' ] ) {
                $item[ 'like' ] = count( explode( ',', $item[ 'like' ] ) );
            }
        }

        $commentstr = $commentlist->render();

        $this->assign( [
            'count' => $count,
            'commentlist' => $commentlist,
            'commentstr' => $commentstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    /* 软删评论 */

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            // 软删除
            $result = $this->CommentModel->destroy( $ids );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->UserModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '添加回收站成功！';
            }

            return json( $success );
        }
    }

    // 回收站

    public function recycle() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'comment.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'comment.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'comment.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'post.title' ] = [ 'like', "%$keywords%" ];
        }

        // 查询帖子总数
        $count = $this->CommentModel->onlyTrashed()->count();

        $commentlist = $this->CommentModel->onlyTrashed()->with( [ 'post', 'user' ] )->where( $where )->order( 'createtime', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $commentlist as $item ) {
            if ( $item[ 'like' ] ) {
                $item[ 'like' ] = count( explode( ',', $item[ 'like' ] ) );
            }
        }

        $commentstr = $commentlist->render();

        $this->assign( [
            'count' => $count,
            'commentlist' => $commentlist,
            'commentstr' => $commentstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 取消软删除 恢复正常

    public function recovery() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            $data = [
                'deletetime' => null,
            ];

            // 软删除
            $result = $this->CommentModel->onlyTrashed()->where( $where )->update( [ 'deletetime' => NULL ] );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->CommentModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '恢复评论成功';
            }

            return json( $success );
        }

    }

    // 真正删除

    public function cut() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->CommentModel->onlyTrashed()->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->CommentModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除评论成功';
            }

            return json( $success );
        }
        return '456';
    }

}
?>